import { createStore } from 'vuex'

export default createStore({
  state: {
    messages: [],
    loading: false
  },
  mutations: {
    SET_MESSAGES(state, messages) {
      state.messages = messages
    },
    ADD_MESSAGE(state, message) {
      state.messages.push(message)
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    }
  },
  actions: {
    setMessages({ commit }, messages) {
      commit('SET_MESSAGES', messages)
    },
    addMessage({ commit }, message) {
      commit('ADD_MESSAGE', message)
    }
  },
  getters: {
    allMessages: state => state.messages,
    isLoading: state => state.loading
  }
})